home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-06-25 | 11.3 KB | 256 lines | [TEXT/CCL ] |
- ; (c) Copyright 1990 by University of Massachusetts. All rights reserved.
- ; This software was conceived, designed, and written by Dan Suthers
- ; while supported by the National Science Foundation under grant number
- ; MDR 8751362, and by a fellowship from Apple Computer, Inc., Cupertino,
- ; CA. Partial support was also received from the Office of Naval Research
- ; under a University Research Initiative Grant, contract N00014-86-K-0764.
- ; Mr. Suthers created this software under his own initiative while in an
- ; academic relationship with the University of Massachusetts. The above
- ; copyright notice was a condition placed by University lawyers on approval
- ; of distribution of this software by Apple Computer, and is not meant to
- ; imply that this software was created in an employment or "work for hire"
- ; relationship between the University and Mr. Suthers.
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; File: Rule-Defs.lisp
- ; Author: Dan Suthers
- ; Created: 19-Oct-88 21:57:32
- ; Modified: 22-Jun-90 02:19:38 (Dan Suthers)
- ; Language: Common Lisp
- ; Package: RULE
- ;
- ; Description: Rule-based reasoner built on the pattern matching facilities
- ; of DNET. Supports forward and backward reasoning.
- ;
- ; This file contains essential definitions only: those which
- ; are used by all other Rule code, or which the user always
- ; needs to create and access data structures. See also
- ; RULES, Rule-Build, Rule-Forward, and Rule-Back.
- ; File RULES has documentation.
- ;
- ; (c) Copyright 1988, by Daniel D. Suthers
- ; Department of Computer and Information Science
- ; University of Massachusetts
- ; Amherst, Massachusetts 01003
- ;
- ; This software was conceived, designed, and written by Dan Suthers
- ; while supported by the National Science Foundation under grant number
- ; MDR 8751362, and by a fellowship from Apple Computer, Inc., Cupertino,
- ; CA. Partial support was also received from the Office of Naval Research
- ; under a University Research Initiative Grant, contract N00014-86-K-0764.
- ; I wish to acknowledge the generous support of Beverly Woolf, who obtained
- ; the above grants and encouraged me to pursue my own research interests in
- ; her lab. This work would not have been possible without the resources and
- ; stimulating environment of the Computer and Information Science department.
- ;
- ; Permission to use, modify, and distribute this software is granted subject
- ; to the following restrictions and understandings:
- ; 1. The file header, including this notice, shall be retained, and may be
- ; extended to include documentation of modifications to the software.
- ; 2. This material is for nonprofit educational and research purposes only.
- ; Users are requested, but not required, to inform Mr. Suthers of any
- ; noteworthy uses of this software.
- ; 3. Mr. Suthers and the University of Massachusetts make no warrantee or
- ; representation that the operation of this software will be error free,
- ; and are under no obligation to provide any services.
- ; 4. Any user of such software agrees to indemnify and hold harmless Mr.
- ; Suthers and the University of Massachusetts from all claims arising
- ; out of the use or misuse of this software, or arising out of any
- ; accident, injury, or damage whatsoever, and from all costs, counsel
- ; fees, and liabilities incurred in or about any such claim, action, or
- ; proceeding brought thereon.
- ; 5. All materials and reports developed as a consequence of the use of
- ; this software shall duly acknowledge such use, in accordance with
- ; the usual standards of acknowledging credit in academic research.
- ;
- ; Status: Working.
- ;
- ; Changes:
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (in-package :RULE)
-
- (export '(
- *rule-trace*
-
- add-datum
- datum-justification
- delete-datum
- make-data-base
- make-rule-base
-
- justification
- justification-warrant
- justification-grounds
- justification-time
-
- defvariables
-
- ;;; Didn't work. Any ideas?
- ;;; ;; These are shadowed by DNET symbols, so it appears that these DNET
- ;;; ;; functions are in the RULE package.
- ;;; all-expressions
- ;;; defvariable
- ;;; variable-p
-
- ))
-
- ;;;(eval-when (eval load)
- ;;; (shadowing-import '(dnet:all-expressions dnet:defvariable dnet:variable-p)))
-
- (use-package :DNET)
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;;
- ;;; ESSENTIAL DATA STRUCTURES
- ;;;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defvar *RULE-TRACE* nil
- "If non-nil, should be a stream to write trace of rule firings to.")
-
- ;;;------------------------------------------------------------------------
- ;;; Needed in this file to write ADD-DATUM.
-
- (defstruct (JUSTIFICATION (:type vector))
- "One of these is associated with each datum to record where it came from."
- (WARRANT nil) ; a rule name
- (GROUNDS nil) ; expressions which made the rule succeed.
- (TIME 0 :type integer)) ; universal time stamp
-
- ;;;------------------------------------------------------------------------
- ;;; The records which are placed in the DNET-TERMINAL-INFOs which tell us
- ;;; about the rule we just matched to. This is a variant record, to save
- ;;; space, since few rules need the bindings list. So I don't use defstruct.
-
- (defun MAKE-RULE-RECORD (rule pattern repeatable)
- (if repeatable (list rule pattern nil) (list rule pattern)))
-
- ;;; Must be able to use for mapcar and :test.
- (defun RULE-RECORD-RULE-NAME (rule-record)
- (first rule-record))
- (defun RULE-RECORD-PATTERN (rule-record)
- (second rule-record))
- (defun RULE-RECORD-REPEATABLE (rule-record)
- (not (cddr rule-record)))
-
- ;;; Must have setf access.
- (defmacro RULE-RECORD-BINDINGS (rule-record)
- `(third ,rule-record))
-
- ;;;------------------------------------------------------------------------
- ;;; Antecedents and consequents are stored in DNETs labeled with :ANTECEDENT
- ;;; and :CONSEQUENT, to keep the matcher from confusing them. Thus, query
- ;;; patterns must have the same form. The following templates eliminate the
- ;;; need for any consing, using (setf (cdr *antecedent-template*) <expression>).
- ;;; Of course we have to be careful where we use something that will be hacked.
-
- (defvar *ANTECEDENT-TEMPLATE* (list :antecedent))
- (defvar *CONSEQUENT-TEMPLATE* (list :consequent))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;;
- ;;; INTERNAL FUNCTIONS AND MACROS
- ;;;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;; Little Helpers
-
- (defun SAME-JUSTIFICATION (j1 j2)
- (declare (vector j1 j2) ; justification is unnamed type
- (optimize (safety 1) (space 2) (speed 3)))
- ;; The are the same if the warrant and grounds are the same.
- (and (eq (justification-warrant j1) (justification-warrant j2))
- (equal (justification-grounds j1) (justification-grounds j2))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;; Processing and Recording data (needed by forward only, but provided
- ;;; for user's use in building datum DNETs regardless of direction.)
-
- (defun ADD-DATUM-INTERNAL (datum dnet warrant grounds)
- ;; Adds <datum> to <dnet> with justification recorded. If already there,
- ;; adds the new justification if it differs from existing justification.
- (let ((justification
- (make-justification :warrant warrant
- :grounds grounds
- :time (get-universal-time))))
- (multiple-value-bind
- (newly-added dnet-terminal)
- (dnet::indexpr-internal datum dnet (list justification))
- (unless newly-added
- (pushnew justification
- (dnet-terminal-info dnet-terminal)
- :test #'same-justification))
- (values newly-added dnet-terminal))))
-
- (defun DELETE-DATUM-INTERNAL (datum dnet)
- (dnet::delexpr-internal datum dnet))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;;
- ;;; USER INTERFACE FUNCTIONS
- ;;;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (defmacro DEFVARIABLES (&rest variables)
- "defvariables &rest <variables> [Macro]
- Defines all the given symbols or strings as variables."
- (cons 'progn
- (mapcar #'(lambda (v) (macroexpand (list 'dnet:defvariable v)))
- variables)))
-
- (defun MAKE-DATA-BASE (name &key indexpr-hook delexpr-hook info)
- "make-data-base <name> &key <indexpr-hook> <delexpr-hook> <info> [Function]
- Identical to DNET:MAKE-DNET (see it for documentation)."
- (dnet:make-dnet name
- :indexpr-hook indexpr-hook
- :delexpr-hook delexpr-hook
- :info info))
-
- (defun MAKE-RULE-BASE (name &key indexpr-hook delexpr-hook info)
- "make-rule-base <name> &key <indexpr-hook> <delexpr-hook> <info> [Function]
- Identical to DNET:MAKE-DNET (see it for documentation)."
- (dnet:make-dnet name
- :indexpr-hook indexpr-hook
- :delexpr-hook delexpr-hook
- :info info))
-
- ;;;-----------------------------------------
- ;;; Processing and Recording datum.
-
- (defun ADD-DATUM (datum dnet &key (warrant :asserted) (grounds nil))
- "add-datum <datum> <dnet> &key warrant grounds [Function]
- Places the <datum> in <dnet> with a justification constructed from the
- given <warrant> (rule, etc.) and <grounds> (antecedent). Returns two
- values: boolean whether added, and a DNET-TERMINAL (like DNET:INDEXPR)."
- (assert (not-a-dotted-list datum) (datum)
- "[RULE:ADD-DATUM] Dotted lists not allowed in DNET: ~S" datum)
- (check-type dnet symbol)
- (assert (sm:gets 'dnet dnet) (dnet) "[RULE:ADD-DATUM] ~S is not a known DNET." dnet)
- (add-datum-internal datum dnet warrant grounds))
-
- (defun DELETE-DATUM (datum dnet)
- "add-datum <datum> <dnet> [Function]
- Removes the <datum> from the <dnet>. Returns two values: boolean whether
- deleted, and a DNET-TERMINAL (like DNET:DELEXPR)."
- (assert (not-a-dotted-list datum) (datum)
- "[RULE:DELETE-DATUM] Dotted lists not allowed in DNET: ~S" datum)
- (check-type dnet symbol)
- (assert (sm:gets 'dnet dnet) (dnet) "[RULE:DELETE-DATUM] ~S is not a known DNET." dnet)
- (delete-datum-internal datum dnet))
-
- (defun DATUM-JUSTIFICATION (datum dnet)
- "datum-justification <datum> <dnet> [Function]
- Returns a justification structure recording the support for <datum> in
- <dnet>. Justifications are maintained by ADD-DATUM and the forward
- chaining functions. Returns NIL if the datum has not been recorded."
- (check-type dnet symbol)
- (assert (sm:gets 'dnet dnet) (dnet)
- "[DNET:DATUM-JUSTIFICATION] ~S is not a known DNET." dnet)
- (multiple-value-bind
- (recorded terminal)
- (dnet::getexpr-internal datum dnet)
- (if recorded (dnet-terminal-info terminal))))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- (provide :rule-defs)
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;; the end.